const { app, BrowserWindow, ipcMain, screen } = require('electron')
// const Player = require('@zhzq/simple-audio-player')
const Player = require('../audio_play_with_node/lib/player')
const path = require('path')
const util = require('./src/util')
let win
// let outOfWindow
let playerMain
let playerSub
let controller = {}
let pitch = 0
let tempo = 1
let volume = 100
Object.defineProperty(controller, 'pitch', {
  get() {
    return pitch
  }, set(v) {
    pitch = v
    if (playerMain) {
      playerMain.setPitch(v)
    }
  }
})
Object.defineProperty(controller, 'tempo', {
  get() {
    return tempo
  }, set(v) {
    tempo = v
    if (playerMain) {
      playerMain.setTempo(v)
    }
  }
})
Object.defineProperty(controller, 'volume', {
  get() {
    return volume
  }, set(v) {
    volume = v
    if (playerMain) {
      playerMain.volume = v
    }
  }
})


function createWindow() {
  win = new BrowserWindow({
    width: 600,
    minWidth: 600,
    maxWidth: 600,
    height: 350,
    minHeight: 350,
    maxHeight: 350,
    resizable: true,
    transparent: true,
    maximizable: false,
    webPreferences: {
      nodeIntegration: true,
      preload: path.resolve(__dirname, './preload.js')
    },
    frame: false,
    show: false,
  })

  win.loadURL('http://localhost:8080').then(() => {
    win.show()
    win.webContents.openDevTools()
    // util.getRandomMusicData().then(obj => {
    //   win.webContents.send('random-music-data', obj)
    // })

  })
  win.on('focus', () => {

    win.setSize(600, 350)
  })


  // outOfWindow = setInterval(() => {
  //   const pt = screen.getCursorScreenPoint()
  //   const bounds = win.getBounds()
  //   if (pt.x < bounds.x || pt.x > bounds.x + bounds.width || pt.y < bounds.y || pt.y > bounds.y + bounds.height) {
  //     win.webContents.send('out-of-window')
  //   }
  // }, 100)
}
app.on('ready', createWindow)

app.on('window-all-closed', () => {
  // if (outOfWindow) {
  //   clearInterval(outOfWindow)
  // }
  app.quit()
})

ipcMain.on('minimize', () => {
  if (win) {
    win.minimize()
  }
})
ipcMain.on('close', () => {
  if (win) {
    win.close()
    win = null
  }
})

ipcMain.on('init-load', () => {
  util.getRandomMusicData().then(obj => {
    playerMain = new Player()
    playerMain.prepare(obj.play_url).then((p) => {
      win.webContents.send('random-music-data', obj)
      setTimeout(() => {
        playerMain.play(f => {
          if (win)
            win.webContents.send('update-time', [util.convertTime(f.currentPos), util.convertTime(f.duration)])
        })
      }, 400)
    })
  })
})

ipcMain.on('volume-changed', (e, value) => {
  controller.volume = value
})
ipcMain.on('pitch-changed', (e, value) => {
  controller.pitch = value

})
ipcMain.on('tempo-changed', (e, value) => {
  controller.tempo = value

})
ipcMain.on('seek', (e, value) => {
  if (playerMain) {
    playerMain.seek(playerMain.duration * value)
  }
})

ipcMain.on('play-or-pause', (e, value) => {
  if (playerMain) {
    if (value) {
      playerMain.resume()
    } else {
      playerMain.pause()
    }
  }

})